package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfo;
import com.atguigu.guli.service.edu.entity.form.CourseSelect;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailVo;
import com.atguigu.guli.service.edu.service.CourseDescriptionService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-10-26
 */
@RestController

@RequestMapping("admin/edu/course")
public class AdminCourseController {
    @Autowired
    CourseService courseService;
    //保存课程的第一步的基本信息
    @Autowired
    CourseDescriptionService courseDescriptionService;

    @PostMapping("save-course-info")
    public R addCourse(@RequestBody CourseInfo courseInfo){
       String courseId =  courseService.saveCourseInfo(courseInfo);
        return R.ok().data("id",courseId);
    }
    //根据id获取对应的数据信息
    @GetMapping("getCourseById/{id}")
    public R getCourse(@PathVariable String id){
        Course course = courseService.getById(id);
        CourseDescription courseDescription = courseDescriptionService.getById(id);
        CourseInfo courseInfo = new CourseInfo();
        BeanUtils.copyProperties(course,courseInfo);
        BeanUtils.copyProperties(courseDescription,courseInfo);
        return R.ok().data("item",courseInfo);
    }
    //跟据id对课程信息进行实时的更新
    @PutMapping("editCourseInfo/{id}")
    public  R editCourseInfo(@RequestBody CourseInfo courseInfo,@PathVariable String id){
        courseService.updateCourse(courseInfo,id);
        return R.ok();
    }
    //查询课程列表
    @GetMapping("getPageCount/{pageNum}/{pageSize}")
    public  R getPageCount(@PathVariable Integer pageNum, @PathVariable Integer pageSize, CourseSelect searchObj){
      Page<CourseDetailVo> page =  courseService.getPageCount(pageNum,pageSize,searchObj);
      return R.ok().data("page",page);
    }
    //课程发布页面
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
      courseService.publishCourseById(id);
       return R.ok();
    }
    //根据id查询课程详情信息
    @GetMapping("getPublishById/{id}")
    public  R getPublishById(@PathVariable String id){
       CourseDetailVo courseDetailVo =  courseService.getCourseInfoById(id);
       return R.ok().data("item",courseDetailVo);
    }


}

